<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/newbase.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>我的红包</title>
    <script>
        $(function () {
            $(".content .right .use img").toggle(
                function () {
                    $(this).attr("src","__STATIC__/img_user/down1.png");
                    $(this).parents(".content").next(".tips").css({display:"none"})
                },
                function () {
                    $(this).attr("src","__STATIC__/img_user/top3.png")
                    $(this).parents(".content").next(".tips").css({display:"block"})
                }

            )
        })
    </script>
</head>
{include file="public/href" /}
<body>
<div class="wrap-yhq">
    <div class="header">
        <p>红包</p>
        <img src="__STATIC__/img/back.png" class="back" onclick="back_url()">
        {eq name=":input('act')" value="act"}
        <span><a href="{:url('mobile/user_info/packet_rule')}">使用规则</a></span>
        {/eq}
    </div>
    <ul class="list">
        <li {eq name=":input('type',2)" value="2"} class="cur" {/eq}><span><a href="?type=2&act={:input('act','')}">未使用(<font id="one_num"> {$count.not_use} </font>)</a></span></li>
        <li {eq name=":input('type',2)" value="1"} class="cur" {/eq}><span><a href="?type=1&act={:input('act','')}">使用记录(<font id="two_num"> {$count.used} </font>)</a></span></li>
        <li {eq name=":input('type',2)" value="3"} class="cur" {/eq}><span><a href="?type=3&act={:input('act','')}">已过期(<font id="there_num"> {$count.out_time} </font>)</a></span></li>
    </ul>
    <input type="hidden" value="{:input('type',2)}" id="type" />
    <div class="tabcon" id="list"></div>
</div>
{include file="public/src" /}
<script>
    //layer流加载
    layui.use('flow', function() {
        var $ = layui.jquery;
        var flow = layui.flow;
        flow.load({
            elem: '#list' //流加载容器
            ,isAuto:true
            ,isLazyimg:true
            ,done: function(page, next){ //执行下一页的回调
                var lis = [];
                var type = $('#type').val();
                var total_price = "{:input('act','')}" == 'order'? sessionStorage.getItem('total_price'):0;
                $.ajax({
                    url:"{:url('mobile/user_info/packet')}",
                    dataType:'json',
                    type:'post',
                    data:{'page':page,'type':type,'total_price':total_price},
                    success:function(t){
                        if (t.count){   //符合确认订单的数量重新赋值
                            $('#one_num').text(t.count.not_use);
                            $('#two_num').text(t.count.used);
                            $('#there_num').text(t.count.out_time);
                        }
                        layui.each(t.data, function(index, item){
                            var is_get = '';
                            var use = '';
                            var id = 'id="ysy"';
                            if (type == 2){
                                is_get = 'class="cur"';
                                use = '<p class="get">去使用</p>';
                                id= 'id="wsy1"'
                            }else if (type == 3){
                                id = 'id="ysx"';
                            }
                            var packet_id = sessionStorage.getItem('red_packet_id');   //已选取的红包id
                            if (packet_id != item.packet_id){    //隐藏选取的红包
                            lis.push('<div '+is_get+' ><div class="center" '+id+' >' +
                                '<div class="content"><div class="left" onclick="to_use('+type+','+item.id+')">' +
                                '<p class="price">￥<span style="font-size: 20px">'+item.actual_price+'</span></p>'+use+'</div>' +
                                '<div class="right"><p class="dress">'+item.title+'</p>' +
                                '<p class="num" style="font-size: 12px">'+item.start_time+'至'+item.end_time+'</p>' +
                                '<p class="use"><span>消费满'+item.virtual_price+'使用 </span>' +
                                '<img src="__STATIC__/img_user/down1.png" id="img_'+item.id+'" onclick="up_down('+item.id+')"></p></div></div>' +
                                '<div class="tips" id="tips_'+item.id+'" style="display: none"><p>'+item.content+'</p></div></div></div>');
                            }else{   //若有选取的红包,未使用数量相应减去1
                                $('#one_num').text(parseInt($('#one_num').text())-1);
                            }
                        });
                        next(lis.join(''), page < t.pages);
                    },
                    error: function () {
                        layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
                    }
                })
            }
        });
    });
    function to_use(type,id){
        var storage = window.sessionStorage;
        if (type == 2){   //未使用
            if ("{:input('act')}" == 'order'){  //如果是订单来源
                var url = sessionStorage.getItem('order_yes_url');
                window.location.href = url+"&red_packet_id="+id+"&red_shop_id="+storage.red_shop_id;
            }else {
                //返回我的主页
                window.location.href = "{:url('mobile/user_port/index')}";
            }
        }else{
            layer.open({content:'无效红包,不可使用',skin:'msg',time:2})
        }
    }
    //返回来源页
    function back_url(){
        if ("{:input('act','')}" == 'order'){   //订单页来源
            window.location.href = sessionStorage.getItem('order_yes_url');
        }else{
            var url = sessionStorage.getItem('red_packet');
            window.location.href = url;
        }
    }
    function up_down(id){
        var $_this = $('#img_'+id);
        var url = $_this.prop('src');
        if (url.substr(url.length-9) == "top31.png"){
            $_this.attr("src","__STATIC__/img_user/down1.png")
            $('#tips_'+id).css({display:"none"})
        }else{
            $_this.attr("src","__STATIC__/img_user/top31.png")
            $('#tips_'+id).css({display:"block"})
        }
    }
</script>

</body>
</html>